#ifndef WEB_SOCKET_SERVER_HPP
#define WEB_SOCKET_SERVER_HPP

#include <boost/asio.hpp>

#include "logger.hpp"
#include "handshake_handler.hpp"
#include "client.hpp"
#include "message_receiver.hpp"
#include "client_factory.hpp"
#include "message_receiver.hpp"

typedef std::map<long, boost::shared_ptr<client> > client_map;

class web_socket_server
{
public:
    web_socket_server(boost::asio::io_service& io_service,
                      int port,
                      boost::shared_ptr<logger> log,
                      boost::shared_ptr<client_factory> factory,
                      const std::string& origin,
                      const std::string& location,
                      boost::shared_ptr<message_receiver> receiver);

    inline handshake_handler& handler() { return _handler; }
    inline boost::shared_ptr<message_receiver> receiver() { return _receiver; }
    inline client_map& clients() { return _clients; }

    void start_accept();

private:
    client_map _clients;
    uint64_t _new_client_index;
    boost::asio::ip::tcp::acceptor _acceptor;
    boost::shared_ptr<logger> _logger;
    boost::shared_ptr<client_factory> _client_factory;
    boost::shared_ptr<message_receiver> _receiver;
    handshake_handler _handler;

    void handle_accept(boost::shared_ptr<client> new_client, const boost::system::error_code& error);
};

#endif // WEB_SOCKET_SERVER_HPP
